From 357311dd400f7f72d2132f2f94161ece39bf08c6 Mon Sep 17 00:00:00 2001
From: hjl <hjl@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Tue, 16 Jan 2018 11:10:44 +0000
Subject: [PATCH 6/9] x86: Add -mfunction-return=

Add -mfunction-return= option to convert function return to call and
return thunks.  The default is 'keep', which keeps function return
unmodified.  'thunk' converts function return to call and return thunk.
'thunk-inline' converts function return to inlined call and return thunk.
'thunk-extern' converts function return to external call and return
thunk provided in a separate object file.  You can control this behavior
for a specific function by using the function attribute function_return.

Function return thunk is the same as memory thunk for -mindirect-branch=
where the return address is at the top of the stack:

__x86_return_thunk:
	call L2
L1:
	pause
	lfence
	jmp L1
L2:
	lea 8(%rsp), %rsp|lea 4(%esp), %esp
	ret

and function return becomes

	jmp __x86_return_thunk

-mindirect-branch= tests are updated with -mfunction-return=keep to
avoid false test failures when -mfunction-return=thunk is added to
RUNTESTFLAGS for "make check".

gcc/

	Backport from mainline
	2018-01-14  H.J. Lu  <hongjiu.lu@intel.com>

	* config/i386/i386-protos.h (ix86_output_function_return): New.
	* config/i386/i386.c (ix86_set_indirect_branch_type): Also
	set function_return_type.
	(indirect_thunk_name): Add ret_p to indicate thunk for function
	return.
	(output_indirect_thunk_function): Pass false to
	indirect_thunk_name.
	(ix86_output_indirect_branch_via_reg): Likewise.
	(ix86_output_indirect_branch_via_push): Likewise.
	(output_indirect_thunk_function): Create alias for function
	return thunk if regno < 0.
	(ix86_output_function_return): New function.
	(ix86_handle_fndecl_attribute): Handle function_return.
	(ix86_attribute_table): Add function_return.
	* config/i386/i386.h (machine_function): Add
	function_return_type.
	* config/i386/i386.md (simple_return_internal): Use
	ix86_output_function_return.
	(simple_return_internal_long): Likewise.
	* config/i386/i386.opt (mfunction-return=): New option.
	(indirect_branch): Mention -mfunction-return=.
	* doc/extend.texi: Document function_return function attribute.
	* doc/invoke.texi: Document -mfunction-return= option.

gcc/testsuite/

	Backport from mainline
	2018-01-14  H.J. Lu  <hongjiu.lu@intel.com>

	* gcc.target/i386/indirect-thunk-1.c (dg-options): Add
	-mfunction-return=keep.
	* gcc.target/i386/indirect-thunk-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-5.c: Likewise.
	* gcc.target/i386/indirect-thunk-6.c: Likewise.
	* gcc.target/i386/indirect-thunk-7.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-5.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-6.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-7.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-8.c: Likewise.
	* gcc.target/i386/indirect-thunk-bnd-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-bnd-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-bnd-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-bnd-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-5.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-6.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-7.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-5.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-6.c: Likewise.
	* gcc.target/i386/indirect-thunk-inline-7.c: Likewise.
	* gcc.target/i386/ret-thunk-1.c: New test.
	* gcc.target/i386/ret-thunk-10.c: Likewise.
	* gcc.target/i386/ret-thunk-11.c: Likewise.
	* gcc.target/i386/ret-thunk-12.c: Likewise.
	* gcc.target/i386/ret-thunk-13.c: Likewise.
	* gcc.target/i386/ret-thunk-14.c: Likewise.
	* gcc.target/i386/ret-thunk-15.c: Likewise.
	* gcc.target/i386/ret-thunk-16.c: Likewise.
	* gcc.target/i386/ret-thunk-2.c: Likewise.
	* gcc.target/i386/ret-thunk-3.c: Likewise.
	* gcc.target/i386/ret-thunk-4.c: Likewise.
	* gcc.target/i386/ret-thunk-5.c: Likewise.
	* gcc.target/i386/ret-thunk-6.c: Likewise.
	* gcc.target/i386/ret-thunk-7.c: Likewise.
	* gcc.target/i386/ret-thunk-8.c: Likewise.
	* gcc.target/i386/ret-thunk-9.c: Likewise.

i386: Don't use ASM_OUTPUT_DEF for TARGET_MACHO

ASM_OUTPUT_DEF isn't defined for TARGET_MACHO.  Use ASM_OUTPUT_LABEL to
generate the __x86_return_thunk label, instead of the set directive.
Update testcase to remove the __x86_return_thunk label check.  Since
-fno-pic is ignored on Darwin, update testcases to sscan or "push"
only on Linux.

gcc/

	Backport from mainline
	2018-01-15  H.J. Lu  <hongjiu.lu@intel.com>

	PR target/83839
	* config/i386/i386.c (output_indirect_thunk_function): Use
	ASM_OUTPUT_LABEL, instead of ASM_OUTPUT_DEF, for TARGET_MACHO
	for  __x86.return_thunk.

gcc/testsuite/

	Backport from mainline
	2018-01-15  H.J. Lu  <hongjiu.lu@intel.com>

	PR target/83839
	* gcc.target/i386/indirect-thunk-1.c: Scan for "push" only on
	Linux.
	* gcc.target/i386/indirect-thunk-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-7.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-5.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-6.c: Likewise.
	* gcc.target/i386/indirect-thunk-attr-7.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-2.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-4.c: Likewise.
	* gcc.target/i386/indirect-thunk-extern-7.c: Likewise.
	* gcc.target/i386/indirect-thunk-register-1.c: Likewise.
	* gcc.target/i386/indirect-thunk-register-3.c: Likewise.
	* gcc.target/i386/indirect-thunk-register-4.c: Likewise.
	* gcc.target/i386/ret-thunk-10.c: Likewise.
	* gcc.target/i386/ret-thunk-11.c: Likewise.
	* gcc.target/i386/ret-thunk-12.c: Likewise.
	* gcc.target/i386/ret-thunk-13.c: Likewise.
	* gcc.target/i386/ret-thunk-14.c: Likewise.
	* gcc.target/i386/ret-thunk-15.c: Likewise.
	* gcc.target/i386/ret-thunk-9.c: Don't check the
	__x86_return_thunk label.
	Scan for "push" only for Linux.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-7-branch@256734 138bc75d-0d04-0410-961f-82ee72b054a4

[Ubuntu note: Dropped indirect-thunk-5.c, indirect-thunk-6.c,
 indirect-thunk-bnd-3.c, indirect-thunk-bnd-4.c,
 indirect-thunk-extern-5.c, indirect-thunk-extern-6.c,
 indirect-thunk-inline-5.c, and indirect-thunk-inline-6.c tests due
 to gcc 5.4 and earlier not supporting the -fno-plt option.
	--sbeattie,]

---
 src/gcc/config/i386/i386-protos.h                           |    1 
 src/gcc/config/i386/i386.c                                  |  152 +++++++++++-
 src/gcc/config/i386/i386.h                                  |    3 
 src/gcc/config/i386/i386.md                                 |    9 
 src/gcc/config/i386/i386.opt                                |    6 
 src/gcc/doc/extend.texi                                     |    9 
 src/gcc/doc/invoke.texi                                     |   13 -
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-1.c        |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-2.c        |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-3.c        |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-4.c        |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-7.c        |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-1.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-2.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-3.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-4.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-5.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-6.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-7.c   |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-8.c   |    2 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-bnd-1.c    |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-bnd-2.c    |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-1.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-2.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-3.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-4.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-7.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-1.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-2.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-3.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-4.c |    4 
 src/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-7.c |    4 
 src/gcc/testsuite/gcc.target/i386/ret-thunk-1.c             |   13 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-10.c            |   23 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-11.c            |   23 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-12.c            |   22 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-13.c            |   22 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-14.c            |   22 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-15.c            |   22 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-16.c            |   18 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-2.c             |   13 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-3.c             |   12 
 src/gcc/testsuite/gcc.target/i386/ret-thunk-4.c             |   12 
 src/gcc/testsuite/gcc.target/i386/ret-thunk-5.c             |   15 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-6.c             |   14 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-7.c             |   13 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-8.c             |   14 +
 src/gcc/testsuite/gcc.target/i386/ret-thunk-9.c             |   24 +
 48 files changed, 507 insertions(+), 66 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-10.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-11.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-12.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-13.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-14.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-15.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-16.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-3.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-4.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-5.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-6.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-7.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-8.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-9.c

Index: b/src/gcc/doc/extend.texi
===================================================================
--- a/src/gcc/doc/extend.texi
+++ b/src/gcc/doc/extend.texi
@@ -4129,6 +4129,15 @@ call and jump to call and return thunk.
 indirect call and jump to inlined call and return thunk.
 @samp{thunk-extern} converts indirect call and jump to external call
 and return thunk provided in a separate object file.
+
+@item function_return("@var{choice}")
+@cindex @code{function_return} function attribute, x86
+On x86 targets, the @code{function_return} attribute causes the compiler
+to convert function return with @var{choice}.  @samp{keep} keeps function
+return unmodified.  @samp{thunk} converts function return to call and
+return thunk.  @samp{thunk-inline} converts function return to inlined
+call and return thunk.  @samp{thunk-extern} converts function return to
+external call and return thunk provided in a separate object file.
 @end table
 
 On the PowerPC, the following options are allowed:
Index: b/src/gcc/doc/invoke.texi
===================================================================
--- a/src/gcc/doc/invoke.texi
+++ b/src/gcc/doc/invoke.texi
@@ -1091,7 +1091,7 @@ See RS/6000 and PowerPC Options.
 -msse2avx -mfentry -mrecord-mcount -mnop-mcount -m8bit-idiv @gol
 -mavx256-split-unaligned-load -mavx256-split-unaligned-store @gol
 -malign-data=@var{type} -mstack-protector-guard=@var{guard} @gol
--mindirect-branch=@var{choice}}
+-mindirect-branch=@var{choice} -mfunction-return=@var{choice}}
 
 @emph{x86 Windows Options}
 @gccoptlist{-mconsole -mcygwin -mno-cygwin -mdll @gol
@@ -24029,6 +24029,17 @@ to external call and return thunk provid
 You can control this behavior for a specific function by using the
 function attribute @code{indirect_branch}.  @xref{Function Attributes}.
 
+@item -mfunction-return=@var{choice}
+@opindex -mfunction-return
+Convert function return with @var{choice}.  The default is @samp{keep},
+which keeps function return unmodified.  @samp{thunk} converts function
+return to call and return thunk.  @samp{thunk-inline} converts function
+return to inlined call and return thunk.  @samp{thunk-extern} converts
+function return to external call and return thunk provided in a separate
+object file.  You can control this behavior for a specific function by
+using the function attribute @code{function_return}.
+@xref{Function Attributes}.
+
 @end table
 
 @c man end
